import json
import time
import requests
import threading
import AudioConfig
import AudioUtils
import os

URL = 'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%s0&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&hd=&latest=&copyright=&word=%s&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&pn=%d&rn=30&gsm=14a&1556881011073='

DOWNLOAD_PATH = AudioConfig.BASE_PATH + '\\image\\'

timer = None


def download_file(path):
    data = requests.get(path).content
    with open(DOWNLOAD_PATH + str(time.time()) + ".jpg", 'wb+') as i:
        i.write(data)


def get_image(keyword, page=3):
	AudioConfig.set_image(False)
	if not os.path.exists(DOWNLOAD_PATH):
		os.mkdir(DOWNLOAD_PATH)

	for i in range(1, page+1):
		print('正在采集第%d页，每页共30张，共%d张' % (i, page))
		body = requests.get(URL % (keyword, keyword, i),
							allow_redirects=False, headers={
			'Cookie': 'BIDUPSID=0E6D328A258B2097DFAAB4881EBCF87C; PSTM=1642508593; BAIDUID=0E6D328A258B2097612D8F4F795CDFEA:FG=1; MCITY=-%3A; H_WISE_SIDS=107314_110085_127969_179348_184716_188332_188742_189755_190619_190800_191068_191253_191370_192383_192958_194085_194511_194530_195342_195467_196050_196428_196881_197241_197711_197958_198263_198418_199082_199177_199468_199576_199597_200150_200349_200744_200993_201054_201191_201548_201600_201702_201978_202131_202175_202297_202759_202892_203191_203274_203280_203360_203606_204032_204101_204132_204154_204322_204431_204676_204724_204824_204859_204910_204940_204972_205009_205087_205095_205218_205236_205239_205329_205338_205484_205809_205831_205918_206189_206249_8000053_8000099_8000126_8000141_8000143_8000150_8000159_8000165_8000171_8000178_8000176_8000186; RT="z=1&dm=baidu.com&si=3g2k0zswf5d&ss=l1vhp5jw&sl=0&tt=0&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=5dd8&ul=735un&hd=735vz"; BAIDUID_BFESS=0E6D328A258B2097612D8F4F795CDFEA:FG=1; BDUSS=92dzZzTElHV1hNUThSUGhxdmo0S01YTEVVeU1mLW9YTGtFUS1GTlFpVVZ3SUZpRUFBQUFBJCQAAAAAAAAAAAEAAACN69Zfd3d3eGptNTU1NTVjb20AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABUzWmIVM1piUk; BDUSS_BFESS=92dzZzTElHV1hNUThSUGhxdmo0S01YTEVVeU1mLW9YTGtFUS1GTlFpVVZ3SUZpRUFBQUFBJCQAAAAAAAAAAAEAAACN69Zfd3d3eGptNTU1NTVjb20AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABUzWmIVM1piUk; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm',
			'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
		}).text

		jsonData = json.loads(body)['data']

		for j in range(len(jsonData)):
			print('正在采集第%d页%d张' % (j, j+1))
			if 'thumbURL' in jsonData[j]:
				download_file(jsonData[j]['thumbURL'])

	AudioUtils.say('采集完成啦，本次采集%d页，共%d张图片' % (page, page*30), 6)


def clear_flag():
    if AudioConfig.get_image():
        AudioConfig.set_image(False)
        global timer
        AudioUtils.say('由于您15秒内没有说出想采集的图片,程序已自动关闭图片采集', 7)


def timer_start():
    global timer
    timer = threading.Timer(15, clear_flag)
    timer.start()
